package com.pbd.client;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.pbd.client.utils.LoadingBox;

public class MemberSearchGrid extends Composite {
	private FlexTable topPaginationTable;
	private FlexTable bottomPaginationTable;
	private String whereClause;
	private String[] filterAndOrderExtraColumnExps;
	private List<Map> partyList;
	private FlexTable innerTable;
	int noOfPage = 0;
	public MemberSearchGrid() {
		initUI();
	}

	public MemberSearchGrid(String whereClaue, String[] filterAndOrderExtraColumnExps) {
		this.whereClause = whereClaue;
		this.filterAndOrderExtraColumnExps = filterAndOrderExtraColumnExps;
		initUI();
	}

	public void initUI() {
		final FlexTable mainTable = new FlexTable();
		mainTable.setStyleName("left_blue_box1");
		mainTable.setWidth("660px");
		initWidget(mainTable);

		innerTable = new FlexTable();
		innerTable.setWidth("640px");
		innerTable.setStyleName("white_box");
		innerTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		mainTable.setWidget(0, 0, innerTable);

		ServiceUtil.getMemberServiceAsync().getPartyList(whereClause, filterAndOrderExtraColumnExps, new AsyncCallback<List<Map>>() {

			@Override
			public void onSuccess(List<Map> result) {
				LoadingBox.getSharedInstance().hide();
				if (result.size() > 0) {
					partyList = result;
					System.err.println("partyList : "+partyList.size());
					noOfPage = 0;
					if(result.size() < 4)
						noOfPage = 1;
					else{
					    noOfPage = result.size() /3;
					    if(result.size()%3 != 0)
					    	noOfPage = noOfPage+1;
					}

					if (noOfPage == 0)
						noOfPage = 1;
					if (noOfPage > 10)
						noOfPage = 10;
					setTopPaginationTable();
					innerTable.setWidget(0, 0, topPaginationTable);
					showSelectedPage(1);
					setBottomPaginationTable();
					innerTable.setWidget(2, 0, bottomPaginationTable);
					innerTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
					innerTable.getFlexCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);

				}else{
					innerTable.setWidget(0, 0, new HTML("<span class=\"body_txt_orange\"> No record found </span>"));
					innerTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void setTopPaginationTable() {
		topPaginationTable = new FlexTable();

		if (noOfPage > 1)
			topPaginationTable.setWidget(0, 0, new Image(".\\images\\prev_bt_dis.png"));
		System.err.println("noOfPage:"+noOfPage);
		for (int index = 1; index <= noOfPage; index++) {
			HTML htmlPageIndex = new HTML(String.valueOf(index));
			topPaginationTable.setWidget(0, index, htmlPageIndex);
			addClickPageIndex(htmlPageIndex);
			if (index == 1)
				topPaginationTable.getFlexCellFormatter().setStyleName(0, index, "numLnk_hi");
			else
				topPaginationTable.getFlexCellFormatter().setStyleName(0, index, "numLnk");
		}
		if (noOfPage > 1)
			topPaginationTable.setWidget(0, topPaginationTable.getCellCount(0), new Image(".\\images\\next_bt_hi.png"));

	}
	private void setBottomPaginationTable() {
		bottomPaginationTable = new FlexTable();
		
		if (noOfPage > 1)
			bottomPaginationTable.setWidget(0, 0, new Image(".\\images\\prev_bt_dis.png"));
		for (int index = 1; index <= noOfPage; index++) {
			HTML htmlPageIndex = new HTML(String.valueOf(index));
			bottomPaginationTable.setWidget(0, index, htmlPageIndex);
			addClickPageIndex(htmlPageIndex);
			if (index == 1)
				bottomPaginationTable.getFlexCellFormatter().setStyleName(0, index, "numLnk_hi");
			else
				bottomPaginationTable.getFlexCellFormatter().setStyleName(0, index, "numLnk");
		}
		if (noOfPage > 1)
			bottomPaginationTable.setWidget(0, bottomPaginationTable.getCellCount(0), new Image(".\\images\\next_bt_hi.png"));

	}


	private void addClickPageIndex(final HTML htmlPageIndex) {
		htmlPageIndex.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				int columnCount = topPaginationTable.getCellCount(0);
				for (int index = 0; index < columnCount; index++) {
					Widget widget = topPaginationTable.getWidget(0, index);
					if (widget instanceof HTML) {
						HTML existingPageHtml = (HTML) widget;
						if (existingPageHtml.getText().equalsIgnoreCase(htmlPageIndex.getText())) {
							System.err.println("htmlPageIndex.getText()"+htmlPageIndex.getText());
							showSelectedPage(Integer.valueOf(htmlPageIndex.getText()).intValue());
							htmlPageIndex.removeStyleName("numLnk");
							htmlPageIndex.setStyleName("numLnk_hi");
						} else {
							htmlPageIndex.setStyleName("numLnk");
						}
					}

				}

			}
		});
	}
	
	private void showSelectedPage(int pageNo){
		FlexTable recordTable = new FlexTable();
		recordTable.setWidth("640px");
		recordTable.setStyleName("white_box");
		recordTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);
		int startIndex = (pageNo*3) - 3;
		int endIndex =0;
		
		
		if((partyList.size() - startIndex) > 3 )
			endIndex = (pageNo*3);
		else
			endIndex = partyList.size();
		
		
		List<Map> showList = partyList.subList(startIndex, endIndex);
		if (innerTable.getRowCount() > 1) 
			innerTable.removeCell(1, 0);
		
		Iterator<Map> partyIterator = showList.iterator();
		int index = 1;
		while (partyIterator.hasNext()) {
			Map partyMap = partyIterator.next();
			MemberOverView memberOverView = new MemberOverView(partyMap);
			recordTable.setWidget(index, 0, memberOverView);
			index++;
		}
		
		innerTable.setWidget(1, 0, recordTable);
		
		
	}

}
